package com.example.myapplication.viewModel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.myapplication.api.WeatherRepository
import com.example.myapplication.data.Forecast
import kotlinx.coroutines.launch

class WeatherViewModel (private val repository: WeatherRepository) : ViewModel() {

    private val _weather = MutableLiveData<Forecast>()


    private val _extraWeather = MutableLiveData<List<Forecast>>()
    val extraWeather: LiveData<List<Forecast>>
        get() = _extraWeather

    fun getWeatherForCity(city: String, date: String) {
        viewModelScope.launch {
            try {
                val result = repository.getWeatherForCity(city, date)
                _weather.postValue(result)
            } catch (e: Exception) {
                Log.e("WeatherViewModel", e.message.toString())
            }
        }
    }

    fun getExtraWeather() {
        viewModelScope.launch {
            try {
                val result = repository.getExtraWeather()
                _extraWeather.postValue(result)
            } catch (e: Exception) {
                Log.e("WeatherViewModel", e.message.toString())
            }
        }
    }
}